package cn.javabb.bootdemo.service;

import cn.javabb.bootdemo.entity.WrMpQR;
import cn.javabb.bootdemo.entity.WrMpZR;
import cn.javabb.bootdemo.mapper.local.StLocalMapper;
import cn.javabb.bootdemo.mapper.water.StWaterMapper;
import com.microsoft.sqlserver.jdbc.SQLServerException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author QINB imqinbao@163.com
 * @CreateDate 2019/1/24/024 15:51
 * @Since V1.0
 */
@Service
@Slf4j
public class WaterSynService {
    @Autowired
    StWaterMapper stWaterMapper;
    @Autowired
    StLocalMapper stLocalMapper;

    @Async
    public void MpQRSyn(int day){
        String nowDate = DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss");
        String yesterDay = DateFormatUtils.format(DateUtils.addDays(new Date(),-day),"yyyy-MM-dd HH:mm:ss");
        log.info("[{}]扫描MpQR同步数据....",nowDate);
        //需要同步的数据,往前推一个小时
        List<WrMpQR> remoteList = stWaterMapper.listWaterQR(nowDate,yesterDay);
        int total = 0;
        if(null!=remoteList && remoteList.size()>0){
            total = remoteList.size();
            log.info("获取MpQR数据：{}条。",total);
            Long startTM = System.currentTimeMillis();
            //执行同步
            int n=0;
            for(WrMpQR qr:remoteList){
                n += this.insertMpQR(qr);
            }
            if(n>0){
                if(n == total){
                    log.debug("MpQR同步成功.用时：{}秒,需要同步记录:{},成功:{}",(System.currentTimeMillis()-startTM)/1000,total,n);
                }else{
                    log.info("MpQR同步成功.用时：{}秒,需要同步记录:{},成功:{},失败:{}",(System.currentTimeMillis()-startTM)/1000,total,n,(total-n));
                }
            }

        }else{
            log.info("MpQR无数据同步");
        }
    }
    @Async
    public void MpZRSyn(int day){
        String nowDate = DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss");
        String yesterDay = DateFormatUtils.format(DateUtils.addDays(new Date(),-day),"yyyy-MM-dd HH:mm:ss");
        log.info("[{}]扫描MpZR同步数据....",nowDate);
        //需要同步的数据,往前推一个小时
        List<WrMpZR> remoteList = stWaterMapper.listWaterZR(nowDate,yesterDay);
        int total = 0;
        if(null!=remoteList && remoteList.size()>0){
            total = remoteList.size();
            log.info("获取MpZR数据：{}条。",total);
            Long startTM = System.currentTimeMillis();
            //执行同步
            int n=0;
            for(WrMpZR zr:remoteList){
                n += this.insertMpZR(zr);
            }
            if(n>0){
                if(n == total){
                    log.debug("MpZR同步成功.用时：{}秒,需要同步记录:{},成功:{}",(System.currentTimeMillis()-startTM)/1000,total,n);
                }else{
                    log.info("MpZR同步成功.用时：{}秒,需要同步记录:{},成功:{},失败:{}",(System.currentTimeMillis()-startTM)/1000,total,n,(total-n));
                }
            }

        }else{
            log.info("MpZR无数据同步");
        }
    }
    /**
     *
     * @param mpQR
     * @return
     */
    public int insertMpQR(WrMpQR mpQR){

        try{
            stLocalMapper.insertWaterQROne(mpQR);
            return 1;
        }catch (DataAccessException ex){
            final Throwable cause = ex.getCause();
            if(cause instanceof SQLServerException){
                log.info("SQLServerException:"+cause.getMessage());
            }else{
                log.error(ex.getMessage());
            }
            return 0;
        }

    }
    /**
     *
     * @param mpZR
     * @return
     */
    public int insertMpZR(WrMpZR mpZR){

        try{
            stLocalMapper.insertWaterZROne(mpZR);
            return 1;
        }catch (DataAccessException ex){
            final Throwable cause = ex.getCause();
            if(cause instanceof SQLServerException){
                log.info("SQLServerException:"+cause.getMessage());
            }else{
                log.error(ex.getMessage());
            }
            return 0;
        }

    }
}
